<?php

namespace app\admin\controller;

use app\admin\model\QuestionCatModel;
use app\admin\model\QuestionPhotoModel;
use app\admin\model\QuestionTagLogModel;
use think\Db;
 
class QuestionTagLog extends Base
{

    /**
     * [index 轮播列表]
     * @return [type] [description]
     * @author felix
     */
    public function index(){

        $key = input('id');
        $map = [];
        //$map['is_show'] = 1;
        if($key&&$key!==""){
            $map['question_id'] = $key;
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('question_tag_log')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $goods_cat = new QuestionTagLogModel();
        //dump($map);
        $lists = $goods_cat->getDataByWhere($map, $Nowpage, $limits);
        //dump($lists);
        /*$type = ['1'=>'图文', '2'=>'外链', '3'=>'正常'];
        // dump($lists);
        foreach($lists as $k=>$v)
        {
            $lists[$k]['type'] = $type[$v['type']];
            //$lists[$k]['is_show'] = $v['is_show'] == 1 ? '显示':'隐藏' ;
        }*/
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('question_id', $key);

        //获取分时推荐
        $tag = model('QuestionTagModel')->where('is_show',1)->field('id,name')->select();
        $this->assign('tag',$tag);


        if(input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }




    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionTagLogAdd()
    {
        $key = input('question_id');
        if(request()->isAjax()){

            $param = input('post.');
            //dump($param);die;
            $goods_cat = new QuestionTagLogModel();
            $flag = $goods_cat->insertData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $ids = Db::name('question_tag_log')->where('question_id',$key)->column('tag_id');
        $tag = model('QuestionTagModel')->where('is_show',1)->whereNotIn('id',$ids)->field('id,name')->select();
        $this->assign('tag',$tag);
        $this->assign('question_id', $key);
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionTagLogEdit()
    {
        $goods_cat = new QuestionTagLogModel();
        if(request()->isAjax()){

            $param = input('post.');
            $flag = $goods_cat->updateData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $tag = model('QuestionTagModel')->where('is_show',1)->field('id,name')->select();
        $this->assign('tag',$tag);

        $id = input('param.id');
        $key = input('question_id');
        $this->assign('question_id', $key);
        $this->assign('photo',$goods_cat->getOneData($id));
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionTagLogDel()
    {
        $id = input('param.id');
        $goods_cat = new QuestionTagLogModel();
        $flag = $goods_cat->delData($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }






}